package cool.tianyou.blue.bird.module.synchronize.rest;


import cool.tianyou.blue.bird.common.mapper.DeviceMapper;
import cool.tianyou.blue.bird.common.mapper.UserMapper;
import cool.tianyou.blue.bird.common.model.entity.EncodedMessage;
import cool.tianyou.blue.bird.common.model.entity.Message;
import cool.tianyou.blue.bird.common.model.entity.RestApiResult;
import cool.tianyou.blue.bird.common.model.po.Device;
import cool.tianyou.blue.bird.common.model.po.LoginUser;
import cool.tianyou.blue.bird.common.model.po.User;
import cool.tianyou.blue.bird.common.tool.UserHolder;
import cool.tianyou.blue.bird.common.util.Assert;
import cool.tianyou.blue.bird.common.util.JsonReturn;
import cool.tianyou.blue.bird.common.util.RestResultUtil;
import cool.tianyou.blue.bird.module.auth.service.UserService;
import cool.tianyou.blue.bird.module.synchronize.model.PushResult;
import cool.tianyou.blue.bird.module.synchronize.service.DeviceService;
import cool.tianyou.blue.bird.module.synchronize.service.WebSocketService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liutianyou
 * @date 2021/2/16 11:20 上午
 */
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ApiController {

  private final UserMapper userMapper;
  private final DeviceMapper deviceMapper;
  private final UserService userService;

  private final DeviceService deviceService;
  private final WebSocketService webSocketService;



  @PostMapping("clientPublicKey")
  public ResponseEntity<RestApiResult<String>> publicKey(String key, String deviceName) {
    LoginUser loginUser = UserHolder.getLoginUser();
    User user = userMapper.selectByPrimaryKey(loginUser.getUserId());
    deviceService.updatePublicKey(deviceName, key, loginUser.getUserId());
    int i = userMapper.updateByPrimaryKeySelective(user);
    Assert.isTrue(i > 0, "上传公钥失败");
    return RestResultUtil.ok("");
  }

  @PostMapping("obtainClientPublicKey")
  public ResponseEntity<RestApiResult<List<Device>>> getPublicKey() {
    LoginUser loginUser = UserHolder.getLoginUser();
    List<Device> devices = deviceService.findByUserId(loginUser.getUserId());
    return RestResultUtil.ok(devices);
  }

  @PostMapping("/devices")
  public ResponseEntity<RestApiResult<List<Device>>> devices() {
    LoginUser user = UserHolder.getLoginUser();
    List<Device> devices = deviceService.findAllByUserId(user.getUserId());
    return RestResultUtil.ok(devices);
  }

  @PostMapping("/senderDevice")
  public ResponseEntity<RestApiResult<List<Device>>> senderDevice() {
    LoginUser user = UserHolder.getLoginUser();
    List<Device> devices = deviceService.findAllByUserId(user.getUserId());
    List<Device> senderDevices = devices.stream()
        .filter(device -> Objects.equals(device.getType(), "mobile"))
        .collect(Collectors.toList());
    return RestResultUtil.ok(senderDevices);
  }


  @PostMapping("/toggleStatus")
  public ResponseEntity<RestApiResult<String>> toggleStatus(Integer deviceId) {
    LoginUser user = UserHolder.getLoginUser();
    final Device device = deviceMapper.selectByPrimaryKey(user.getDeviceId());
    Assert.isTrue(device != null && Objects.equals(false, device.getDisabled()), "非法操作");
//    deviceService.toggleStatus(deviceId, user.getUserId());
    return RestResultUtil.ok("");
  }

  @PostMapping("changeAlias")
  public ResponseEntity<RestApiResult<String>> changeAlias(Integer deviceId, String newName) {
    LoginUser user = UserHolder.getLoginUser();
    deviceService.changeAlias(deviceId, newName, user.getUserId());
    return RestResultUtil.ok("");
  }

  @PostMapping("offLine")
  public ResponseEntity<RestApiResult<String>> offLineDevice(Integer deviceId) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.offLineDevice(deviceId, loginUser.getUserId());
    return RestResultUtil.ok("");
  }

  @DeleteMapping("device/{id}")
  public ResponseEntity<RestApiResult<String>> deleteDevice(@PathVariable("id") Integer id) {
    deviceService.delete(id, UserHolder.getLoginUser().getUserId());
    return RestResultUtil.ok("");
  }

  @GetMapping("logout")
  public ResponseEntity<RestApiResult<String>> logout() {
    userService.logOut();
    return RestResultUtil.ok("");
  }

}
